﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DelonixRegiaHMSys.Core.Services;
using DelonixRegiaHMSys.Infrastructure.Data;
using DelonixRegiaHMSys.UI.Models;

namespace DelonixRegiaHMSys.UI.WebForms
{
    public partial class LoginPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            string userID = UserIDtbx.Text;
            string password = Passwordtbx.Text;

            var saservice = new saServices(new EfSystemAccountQuery());
            var accountexist = saservice.DoesTheAccountExist(userID, password);
            var isOnline = saservice.IsUserOnline(userID); 

            if (accountexist == true && isOnline == false)
            {
                var isAdmin = saservice.IsAdminOrMS(userID);
                saservice.UpdateUserStatus(userID, "ON");
                Session["User"] = userID;
                if (isAdmin == true)
                {
                    Server.Transfer("~/WebForms/SystemAccountModule/Administrator.aspx?UserID=" + userID);
                }
                else
                {
                    Server.Transfer("~/WebForms/SystemAccountModule/MainPage.aspx?UserID=" + userID);
                }
            }
            else if (accountexist == true && isOnline == true)
            {
                Statuslbl.Text = "User is already Online!";
            }
            else
            {
                Statuslbl.Text = "Login Unsuccessful! Please try again.";
            }
        }
    }
}